package com.bee.common.core.log.service.impl;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bee.common.core.log.persistence.dao.LogRequestDao;
import com.bee.common.core.log.persistence.entity.LogRequest;
import com.bee.common.core.log.service.ILogRequestService;

@Service
public class LogRequestServiceImp implements ILogRequestService{
	
	private	Logger log = LoggerFactory.getLogger(LogRequestServiceImp.class);

	private	BlockingQueue<LogRequest> basket = new ArrayBlockingQueue<LogRequest>(2000);
	@Autowired
	private LogRequestDao requestDao;

	@Override
	public void addRequestUrlAsyn(LogRequest logRequest) {
		basket.offer(logRequest);
	}
	
	
	@PostConstruct
	public void excuteAsyn(){
		
		Executors.newSingleThreadExecutor().execute(new Runnable() {
			@Override
			public void run() {
				while(true){
					LogRequest notify;
					try {
						notify = basket.take();
						requestDao.save(notify);
					}catch(Exception e){
						log.error("错误信息",e);
					}
				}
			}
		});	
	}
}
